package org.tgcloud.group.service.manager.project;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.entity.ProjectMediaRequirements;
import org.tgcloud.group.repository.MjOrderMapper;
import org.tgcloud.group.repository.ProjectMediaRequirementsMapper;
import org.tgcloud.group.repository.SysLabelValueMapper;
import org.tgcloud.group.service.manager.notify.NotifyService;
import org.tgcloud.group.web.controller.dto.project.IntendedMedia.ProjectIntendedMediaDTO;
import org.tgcloud.group.web.controller.dto.project.mediaRequirementsDTO.MediaRequirementsManageDTO;
import org.tgcloud.group.web.controller.req.project.mediaRequirements.MediaRequirementsBatchCreateReq;
import org.tgcloud.group.web.controller.req.project.mediaRequirements.MediaRequirementsCreateReq;
import org.tgcloud.group.web.controller.req.project.mediaRequirements.MediaRequirementsUpdateReq;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @Created: 2025-09-02-上午11:40
 * @Description:
 */
@Service
public class ProjectMediaRequirementsManageService extends ServiceImpl<ProjectMediaRequirementsMapper, ProjectMediaRequirements> {

    @Autowired
    private ProjectMediaRequirementsMapper mediaRequirementsMapper;

    @Autowired
    private MjOrderMapper orderMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    @Autowired
    private NotifyService notifyService;

    public APPResultMessage<Object, MediaRequirementsManageDTO> getProjectMediaRequirementsList(Integer pageNum, Integer pageSize, Integer projectId, Short status) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<MediaRequirementsManageDTO> list = mediaRequirementsMapper.selectManageListByProjectId(projectId, status);
            PageInfo<MediaRequirementsManageDTO> tables = new PageInfo<>(list);
            for (MediaRequirementsManageDTO dto : tables.getList()){
                dto.setBrandPlacementTypeName(sysLabelValueMapper.selectValueByIds(dto.getBrandPlacementType().split(",")));
            }
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, MediaRequirementsManageDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        } else {
            List<MediaRequirementsManageDTO> list = mediaRequirementsMapper.selectManageListByProjectId(projectId, status);
            for (MediaRequirementsManageDTO dto : list){
                dto.setBrandPlacementTypeName(sysLabelValueMapper.selectValueByIds(dto.getBrandPlacementType().split(",")));
            }
            ResultMessage result = ResultMessage.builder().setList(list);
            APPResultMessage<Object, MediaRequirementsManageDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }
    }


    public APPResultMessage doAddProjectMediaRequirements(MediaRequirementsCreateReq req) {
        ProjectMediaRequirements vo = new ProjectMediaRequirements();
        BeanUtils.copyProperties(req, vo);
        vo.setStatus((short) 1);
        vo.setCreateTime(new Date());
        mediaRequirementsMapper.insertSelective(vo);
        doUpdateOrderResourcesRemarkByProjectId(req.getProjectId());
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage addProjectMediaRequirementsCreate(MediaRequirementsBatchCreateReq req) {
        req.getProjectMediaRequirements().stream()
                .forEach(item -> {
                    item.setStatus((short) 1);
                    item.setCreateTime(new Date());
                });
        this.saveBatch(req.getProjectMediaRequirements());
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage doUpdateProjectMediaRequirements(MediaRequirementsUpdateReq req, Integer userId, Short userType) {
        ProjectMediaRequirements vo = new ProjectMediaRequirements();
        BeanUtils.copyProperties(req, vo);
        vo.setStatus((short) 1);
        vo.setCreateTime(new Date());
        vo.setUpdateId(req.getId());
        vo.setUpdateTime(new Date());
        mediaRequirementsMapper.insertSelective(vo);
        mediaRequirementsMapper.updateStatusById(req.getId(), (short) 2);
        doUpdateOrderResourcesRemarkByProjectId(req.getProjectId());
        String templateCode = "updateMediaRequirements";
        Map<String, Object> templateParams = new HashMap<>();
        notifyService.doSendNotifyByRoleId(userId, userType, templateCode, templateParams, req.getProjectId());
        return APPResultMessage.getSuccessMessage();
    }

    // 修改资源要求
    public void doUpdateOrderResourcesRemarkByProjectId(Integer projectId){
        String resourcesRemark1 = "";
        String resourcesRemark2 = "";
        String resourcesRemark1and2 = "";
        String resourcesRemark = "";
        List<MediaRequirementsManageDTO> mediaList = mediaRequirementsMapper.selectUsedListByProjectId(projectId);
        if (mediaList != null && mediaList.size() > 0){
            for (MediaRequirementsManageDTO dto : mediaList){
                String remark = "媒体类型：" + dto.getMediumType() + "。"
                        + "城市：" + dto.getCity() + "。"
                        + "预计上刊时间：" + dto.getOrderTime() + "。"
                        + (StringUtil.isNotBlank(dto.getRemark()) ? ("备注：" + dto.getRemark() + "。") : "")
                        + "\n";
                // 1、自营，2、外采
                if (dto.getStationType().equals("自营")){
                    resourcesRemark1 = resourcesRemark1 + remark;
                } else if (dto.getStationType().equals("外采")){
                    resourcesRemark2 = resourcesRemark2 + remark;
                } else if (dto.getStationType().equals("自营,外采") || dto.getStationType().equals("外采,自营")){
                    resourcesRemark1and2 = resourcesRemark1and2 + remark;
                }
            }
            if (StringUtil.isNotBlank(resourcesRemark1)){
                resourcesRemark = resourcesRemark + "自营：" + resourcesRemark1 + "\n";
            }
            if (StringUtil.isNotBlank(resourcesRemark2)){
                resourcesRemark = resourcesRemark + "外采：" + resourcesRemark2 + "\n";
            }
            if (StringUtil.isNotBlank(resourcesRemark1and2)){
                resourcesRemark = resourcesRemark + "自营+外采：" + resourcesRemark1and2 + "\n";
            }
            orderMapper.updateResourcesRemarkByProjectId(projectId, resourcesRemark);
        }
    }

    public APPResultMessage getProjectMediaRequirements(Integer id){
        ProjectMediaRequirements projectMediaRequirements = mediaRequirementsMapper.selectById(id);
        ResultMessage result = ResultMessage.builder().setData(projectMediaRequirements);
        APPResultMessage<Object, MediaRequirementsManageDTO> message = APPResultMessage.getSuccessMessage();
        message.setResult(result);
        return message;
    }

}
